package com.web_bloks.content.client;

import com.web_bloks.user.client.User;
import com.web_bloks.system.client.Logger;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
abstract public class ConditionChecker {

	public enum Code {
		OK, INVALID, LOGIN_REQUIRED, ADMIN_REQUIRED
	}
	
	private User currentUser = null;

	/*****************************************
	 * CONSTRUCTOR
	 *****************************************/
	protected ConditionChecker() {}
	
	/*****************************************
	 * CONFIGURATION
	 *****************************************/
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}
	/*****************************************
	 * API
	 *****************************************/
	public ConditionCode verify(Condition[] conditions) {
		Logger.logDebug(this.getClass()+".verify");
		
		if (null==conditions) return ConditionCode.OK;
		
		ConditionCode result = ConditionCode.OK;
		
		for(int i=0;i<conditions.length;i++) {
			result = verify(conditions[i].getCode());
			if (result != ConditionCode.OK) break;
		}
		
		return result;
	}
	public ConditionCode verify(Condition.Code code) {
		Logger.logDebug(this.getClass()+".verify");
		
		if (null==code) return ConditionCode.INVALID;
		
		ConditionCode result = ConditionCode.OK;
		
		switch(code) {
		case LOGIN_REQUIRED:
			if (!currentUser.isLogin()) 
				result = ConditionCode.LOGIN_REQUIRED;
			break;
		case ADMIN_REQUIRED:
			if (!currentUser.isAdmin())
				result = ConditionCode.ADMIN_REQUIRED;
			break;
		default:
			throw new RuntimeException( this.getClass()+".verify: invalid code" );
		}
		
		return result;
	}
	/*****************************************
	 * HELPERS
	 *****************************************/

	
}//END
